package com.medsys.appservice;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import net.sf.jasperreports.engine.design.JasperDesign;
import net.sf.jasperreports.engine.xml.JRXmlLoader;
import org.apache.commons.collections.map.HashedMap;

import com.medsys.model.Atestado;
import com.medsys.model.Consulta;
import com.medsys.model.Paciente;
import com.medsys.util.Funcao;

public class RelatorioAppService {
	
	private ConsultaAppService consultaAppService = new ConsultaAppService();
	private PacienteAppService pacienteAppService = new PacienteAppService();
	private AtestadoAppService atestadoAppService = new AtestadoAppService();
	private JasperReport relatorio;
	private JasperDesign design;
	private JasperPrint imprimir;
	private JRBeanCollectionDataSource JRds;
	private static RelatorioAppService relatorioAppService;
	
    private RelatorioAppService(){    	
    }
    
    public static RelatorioAppService getInstance(){
    	if(relatorioAppService == null){
    		relatorioAppService = new RelatorioAppService();
    	}
    	return relatorioAppService;
    }

	@SuppressWarnings("unchecked")
	public void relatorioAgendamentosPorPeriodo(String dataInicio,
			String dataFim) throws JRException {

		

		List<Consulta> consultas = consultaAppService.pesquisarPorPeriodo(
				Funcao.formataData(dataInicio, true),
				Funcao.formataData(dataFim, true));
		
		@SuppressWarnings("rawtypes")
		Map parametros = new HashedMap();
		parametros.put("Title", "Agendamentos por periodo");
		parametros.put("dataInicio", dataInicio);
		parametros.put("dataFim", dataFim);

		design = JRXmlLoader.load("ListaConsultas.jrxml");

		relatorio = JasperCompileManager.compileReport(design);

		JRds = new JRBeanCollectionDataSource(
				consultas);

		imprimir = JasperFillManager.fillReport(relatorio,
				parametros, JRds);
		JasperExportManager.exportReportToPdfFile(imprimir,
				"AgendamentoPorPeriodo.pdf");

	}

	@SuppressWarnings("unchecked")
	public void gerarPdfComListaDePacientes() throws JRException {

		
		List<Paciente> pacientes = pacienteAppService.listarTodos();

		@SuppressWarnings("rawtypes")
		Map parametros = new HashedMap();
		parametros.put("Title", "Pacientes");

		design = JRXmlLoader.load("ListaPacientes.jrxml");

		relatorio = JasperCompileManager.compileReport(design);

		JRds = new JRBeanCollectionDataSource(
				pacientes);

		imprimir = JasperFillManager.fillReport(relatorio,
				parametros, JRds);
		JasperExportManager.exportReportToPdfFile(imprimir,
				"ListaDePacientes.pdf");

	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	public void gerarPdfComHistoricoDoPaciente(Long id)
			throws JRException {

		
		Paciente paciente = pacienteAppService.listarHitoricoMedicoPaciente(id);

		List<Consulta> consultas = paciente.getListCosultas();

		Map parametros = new HashedMap();
		parametros.put("Title", "Histórico do paciente");

		design = JRXmlLoader.load("HistoricoMedico.jrxml");

		relatorio = JasperCompileManager.compileReport(design);

		JRds = new JRBeanCollectionDataSource(
				consultas);

		imprimir = JasperFillManager.fillReport(relatorio,
				parametros, JRds);
		JasperExportManager.exportReportToPdfFile(imprimir,
				"HistoricoDoPaciente.pdf");

	}

	@SuppressWarnings("unchecked")
	public void gerarPdfReceituario(Long id) throws JRException {

		
		Consulta consulta = consultaAppService.recuperarConsultaERelatorios(id);

		@SuppressWarnings("rawtypes")
		Map parametros = new HashedMap();

		parametros.put("Title", "Receituário");

		design = JRXmlLoader.load("Receituario.jrxml");

		relatorio = JasperCompileManager.compileReport(design);

		JRds = new JRBeanCollectionDataSource(
				consulta.getReceiturarios());

		imprimir = JasperFillManager.fillReport(relatorio,
				parametros, JRds);
		JasperExportManager.exportReportToPdfFile(imprimir, "Receituario.pdf");

	}
	@SuppressWarnings("unchecked")
	public void gerarPdfAtestado(Long id) throws JRException{
		List<Consulta> consultas = null;
		Atestado atestado = atestadoAppService.pesquisarPorIdCarregandoPaciente(id);
		@SuppressWarnings("rawtypes")
		Map parametros = new HashedMap();

		parametros.put("titulo", "Atestado Médico");
		parametros.put("nomePaciente", atestado.getReceituario().getConsulta().getPaciente().getNome());
		parametros.put("diasDeAbono", atestado.getDiasDeAbono());
		parametros.put("texto", atestado.getTexto());

		design = JRXmlLoader.load("Atestado.jrxml");

		relatorio = JasperCompileManager.compileReport(design);

		JRds = new JRBeanCollectionDataSource(consultas);

		imprimir = JasperFillManager.fillReport(relatorio, parametros);
		JasperExportManager.exportReportToPdfFile(imprimir, "Atestado.pdf");
	}

}